#!/usr/bin/env php
<?php
function call($method, $request=null)
{/*{{{*/
    $host="localhost";
    $port=80;
    $server="/sample/ws/xmlrpc_server.php";

    $request_e=xmlrpc_encode_request($method, $request,
            array('escaping'=>array('markup'),'encoding'=>'utf-8'));
    print_r($request_e);
    print "----------------------------------------\n";

    if(($fp=fsockopen($host, $port, $errno, $errstr, 5)) === false)
    {
        print "Error: connect fail: $errno $errstr\n";
        return false;
    }
    $query="POST $server HTTP/1.0\n".
        "User_Agent: XML-RPC Client\n".
        "Host:$host.\n".
        "Content-Type:text/xml\n".
        "Content-Length:".strlen($request_e)."\n\n".
        "$request_e\n";
    if(fputs($fp, $query, strlen($query)) === false)
    {
        print "Error: write fail\n";
        return false;
    }
    $response_e='';
    while(!feof($fp))
    {
        $response_e.=fgets($fp);
    }
    fclose($fp);
    print_r($response_e);
    print "----------------------------------------\n";

    $xml=strstr($response_e, '<?xml');
    $response=xmlrpc_decode($xml);
    print_r($response);
    print "\n";
    print "========================================\n";
}/*}}}*/

function main()
{/*{{{*/
    call('HELLO');
    call('HELLO', array('阿隆','arron'));

    $request=array('id'=>100, 'name'=>'阿隆',
            'phone'=>array('home'=>'12345678','mobile'=>'13500000000'));
    call('ECHO', $request);
}/*}}}*/

$code=main();
exit($code);
?>
